package logic

import (
	"context"

	"user_rpc/internal/svc"
	"user_rpc/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserByIDLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserByIDLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByIDLogic {
	return &GetUserByIDLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserByIDLogic) GetUserByID(in *user.IDRequest) (*user.UserInfo, error) {
	idUser, err := l.svcCtx.UserModel.FindOne(l.ctx, in.Id)
	if err != nil {
		l.Logger.Errorf("GetUserByID failed: id=%d, error: %v", in.Id, err)
		return nil, err
	}
	return &user.UserInfo{Id: idUser.Id, Name: idUser.Name, Password: idUser.Password}, nil
}
